package components.dialogs
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_button;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import classes.clt_events;
	
	import components.clt_previewItem;
	import components.pj_hscrollArea;
	
	import data.clt_data;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_headsDialog extends clt_dialog {
		protected var _itemsLayer	: pj_hscrollArea;
		private var _headList		: Array;
		private var _delButton		: uif_button;
		public function clt_headsDialog(parent:uif_component, list: Array) {
			super(parent, 0, 0, 570, 255, false);
			_headList = list;
			
			title.text = seApp.classes.locale.HEADSTITLE;
			var inner: Rectangle = this.getInner();
			_itemsLayer = new pj_hscrollArea(this, inner.x, inner.y, inner.width, 200, 'items');
			_itemsLayer.addEvent(Event.CHANGE, doSelect);
			assignList(_headList);
		}
		
		protected function doSelect(e: Event): void {
			var recObj: Object = _itemsLayer.selectItem.object;
			_delButton.disable = recObj == null;
			if (recObj != null) workspace.dispatchEvent(new ObjectEvent(clt_events.SETHEAD, recObj));
		}
		
		override protected function createDialogComponents(): void {
			super.createDialogComponents();
			addButton('Сохранить', function(e: Event): void {
				var result: Object = {};
				workspace.dispatchEvent(new ObjectEvent(clt_events.GETHEAD, result));
				if (result.image) {
					seApp.internalServer.addHead(seApp.externalServer.viewer_id, 
						result.image, result.maskID, result.restoreInfo, function(a_data: Object): void {
						if (a_data.response[0]) {
							var recID: Object = a_data.response[0];
							
							var preview: Bitmap = se_util.bmpCopy(result.bmp, 100 / result.bmp.height);
							seApp.internalServer.collectorImage(
								'head_preview/' + recID + clt_data.JPGEXT,
								clt.encodeFromJPG(preview.bitmapData),
								function(collect_result: Object): void {
									if (collect_result.response[0].file) {
										var rec: Object = uif_util.clone(result);
										rec.id 			= recID;
										rec.restoreInfo = uif_util.clone(result.restoreInfo);
										_headList.push(rec);
										assignList(_headList);
									}
							});
						}
					});
				}
			});
			_delButton = addButton('Удалить', function(e: Event): void {
				seApp.internalServer.removeHead(_itemsLayer.selectItem.object.id, function(a_data: Object): void {
					_headList.splice(_itemsLayer.selectIndex, 1);
					assignList(_headList);
				});
			});
			addButton('Закрыть', function(e: Event): void {
				close();
			});
			
			_delButton.disable = true;
		}
		
		
		protected function assignList(a_list: Array): void {
			var rows: int = 2;
			a_list = [].concat(a_list);
			var item_width: Number = _itemsLayer.calcFrameWidth(rows);
			while (a_list.length < (Math.floor(_itemsLayer.displayRect.width / item_width) * rows)) a_list.push(null);
			_itemsLayer.assignList(a_list, item_width, function (_scroll: uif_component, inner: Rectangle, index: int): clt_previewItem {
				return new clt_previewItem(index, _scroll, inner.x, inner.y, inner.width, inner.height, 
										a_list[index], 'userPhoto', clt_servers.HEADSURL);
			}, rows);
			
			_delButton.disable = true;
		}
	}
}